/**
 *
 * 投影视频
 * @param     viewer                Mars3d实例对象
 */

import maskImage from './video-mask.png'
import {
    BQGIS_video
} from './BQGIS-video'

import {
    globalLayer,
    globalTool
} from '../BQGIS-globalVar'

import {
    BQGIS_initRoamingByKey
} from "../BQGIS-roamingKey"


export function initVideo3d(viewer) {
    if (!globalLayer.video3dLayer) {
        globalLayer.video3dLayer = new mars3d.layer.GraphicLayer()
        viewer.addLayer(globalLayer.video3dLayer)
    }
    if (!globalTool.video3dDom) {
        globalTool.video3dDom = new BQGIS_video()
    }
}

function createVideoElement(id, src) {
    const videoElement = globalTool.video3dDom.createVideoElement(id)
    if (!videoElement) return
    if (src) {
        globalTool.video3dDom.setVideoSrc({
            src,
            videoElement
        })
    }
    return videoElement
}

/**
 *
 * 添加3d投影
 * 
 * 
 * @param       name        名称
 * @param       url         视频地址
 * @param       position    坐标
 * @param       id          id
 * @param       angle       水平张角
 * @param       angle2      垂直张角
 * @param       heading     翻滚角
 * @param       pitch       俯仰角
 * @param       roll        滚转角
 * @param       distance    投射距离
 * @param       showFrustum 是否显示投影线
 */

export function add3DProjection(option) {
    console.log(option)
    const videoElement = createVideoElement(option.id, option.url)
    if (!videoElement) return
    let video3D = getGraphicById(option.id)
    if (!video3D) {
        const position = typeof option.position === 'string' ? option.position.split(',').map(item => Number(item)) : option.position
        video3D = new mars3d.graphic.Video3D({
            id: option.id,
            position: position,
            style: {
                container: videoElement,
                angle: option.angle,
                angle2: option.angle2,
                heading: option.heading,
                pitch: option.pitch,
                roll: option.roll,
                distance: option.distance,
                showFrustum: option.showFrustum,
                opacity: option.opacity,
                maskImage
            }
        })
        globalLayer.video3dLayer.addGraphic(video3D)
    }
    return video3D
}

export function drawVideo3D(url) {
    const videoElement = createVideoElement(getUUId(), url)
    if (!videoElement) return
    return globalLayer.video3dLayer.startDraw({
        type: "video3D",
        style: {
            container: videoElement,
            showFrustum: true
        }
    })
}

export function removeVideo3D(graphic, viewer = window.viewer) {
    if (!graphic) return
    globalTool.video3dDom.removeVideoElement(graphic.id)
    globalLayer.video3dLayer.removeGraphic(graphic)
}

export function getGraphicById(id) {
    if (globalLayer.video3dLayer) {
        return globalLayer.video3dLayer.getGraphicById(id)
    }
}

export function removeVideo3DById(id, viewer = window.viewer) {
    const graphic = getGraphicById(id)
    if (!graphic) return
    globalTool.video3dDom.removeVideoElement(id)
    globalLayer.video3dLayer.removeGraphic(graphic)
}

export function clear(viewer = window.viewer) {
    globalLayer.video3dLayer.clear(true)
    globalTool.video3dDom.destroy()
}

export function flyTo({
    position,
    heading,
    pitch,
    roll,
    viewer = window.viewer,
    complete = () => {}
}) {
    viewer.camera.flyTo({
        destination: mars3d.LngLatPoint.toCartesian(position), // 设置位置
        orientation: { // 设置角度
            heading: Cesium.Math.toRadians(heading),
            pitch: Cesium.Math.toRadians(pitch),
            roll: Cesium.Math.toRadians(roll),
        },
        complete
    })
}
let cameraChangeEvent = null
export function startCameraViewEdit({
    graphic,
    viewer = window.viewer,
    onChange
}) {
    console.log(graphic.position, mars3d.LngLatPoint.toArray(graphic.position), 'graphic.position')
    viewer.camera.setView({
        destination: graphic.position, // 设置位置
        orientation: { // 设置角度
            heading: Cesium.Math.toRadians(graphic.heading),
            pitch: Cesium.Math.toRadians(graphic.pitch),
            roll: Cesium.Math.toRadians(graphic.roll),
        }
    })
    if (!window.roamingByKey) {
        window.roamingByKey = BQGIS_initRoamingByKey({
            viewer,
        })
    }
    if (window.roamingByKey.status === "doing") return
    window.roamingByKey.start()
    viewer.camera.percentageChanged = 0.01
    if (cameraChangeEvent) {
        cameraChangeEvent()
    }
    cameraChangeEvent = viewer.camera.changed.addEventListener(() => {
        let position = mars3d.LngLatPoint.toArray(viewer.camera.positionWC)
        let heading = Cesium.Math.toDegrees(viewer.camera.heading)
        let pitch = Cesium.Math.toDegrees(viewer.camera.pitch)
        let roll = Cesium.Math.toDegrees(viewer.camera.roll)
        graphic.position = position
        graphic.heading = heading
        graphic.pitch = pitch
        graphic.roll = roll
        if (onChange) {
            onChange({
                heading,
                pitch,
                roll,
                position
            })
        }
    })
}


export function stopCameraViewEdit() {
    if (window.roamingByKey) {
        window.roamingByKey.stop()
        cameraChangeEvent()
    }
}



// 禁止相机默认行为
export function disableCamera(viewer, bool) {
    viewer.scene.screenSpaceCameraController.enableRotate = bool;
    viewer.scene.screenSpaceCameraController.enableTranslate = bool;
    viewer.scene.screenSpaceCameraController.enableZoom = bool;
    viewer.scene.screenSpaceCameraController.enableTilt = bool;
    viewer.scene.screenSpaceCameraController.enableLook = bool;
}

export function changeUrl(graphic, url) {
    globalTool.video3dDom.changeVideoSrc({
        src: url,
        videoElement: graphic.style.container
    })
}

export function changeVideo(graphic, option) {
    for (let key in option) {
        if (option[key] !== undefined && option[key] !== graphic[key]) {
            if (key === 'url') {
                changeUrl(graphic, option.url)
            } else {
                graphic[key] = option[key]
            }
        }
    }
}

function getUUId() {
    let tem = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    let id = "",
        len = tem.length,
        i = 0
    for (i; i < len; i++) {
        if (tem[i] == "-") id += tem[i]
        else {
            let num = Math.floor(Math.random() * 16)
            id += num.toString(16)
        }
    }
    return id
}